const express = require('express')
const path = require('path')
const cookieParser = require('cookie-parser')
const bodyParser = require('body-parser')
const birds = require('./birds')
const app = express()
const port = 3000

app.use('/static', express.static('public'))
app.use('/birds', birds)
app.use(cookieParser())
// 解析 application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// 解析 application/json
app.use(bodyParser.json())

// view engine setup
app.engine('art', require('express-art-template'))
app.set('view options', {
  debug: process.env.NODE_ENV !== 'production'
})
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'art')

// routes
app.get('/', function (req, res) {
  res.render('index.art', {
    user: {
      name: 'aui',
      tags: ['art', 'template', 'nodejs']
    }
  })
})

app.post('/index', (req, res,  next) => {
  console.log(2);
  console.log('req.query', req.query);
  console.log('req.body', req.body);
  res.send(`post：${JSON.stringify(req.body)}`)
})

app.use(function(err, req, res, next) {
  console.error('err', err.stack)
  res.status(500).send('Something broke!')
})
app.all('/', function (req, res, next) {
  console.log('Accessing the secret section ...')
  next(); // pass control to the next handler
})

// 一个中间件栈，处理指向 /user/:id 的 GET 请求
app.get('/user/:id', function (req, res, next) {
  // 如果 user id 为 0, 跳到下一个路由
  if (req.params.id == 0) next('route')
  // 否则将控制权交给栈中下一个中间件
  else next() //
}, function (req, res, next) {
  // 渲染常规页面
  res.send('regular')
});

// 处理 /user/:id， 渲染一个特殊页面
app.get('/user/:id', function (req, res, next) {
  res.send('special')
})


app.get('/', (req, res) => {
  res.send('Hello World!')
})

// 匹配 /random.text 路径的请求
app.get('/random.txt', function (req, res) {
  res.send('random.txt');
});

app.post('/', (req, res, next) => {
  res.send('Got a POST request')
})

app.put('/user', function (req, res) {
  res.send('Got a PUT request at /user')
})

app.delete('/user', function (req, res) {
  res.send('Got a DELETE request at /user')
})

// 匹配 abcd、abbcd、abbbcd等
app.get('/ab+cd', function(req, res) {
  res.send('ab+cd');
});

// 匹配 abcd、abxcd、abRABDOMcd、ab123cd等
app.get('/ab*cd', function(req, res) {
  res.send('ab*cd');
});

// 匹配 /abe 和 /abcde
app.get('/ab(cd)?e', function(req, res) {
  res.send('ab(cd)?e');
});

// 匹配任何路径中含有 a 的路径：
// app.get(/a/, function(req, res) {
//   res.send('/a/');
// });

// 匹配 butterfly、dragonfly，不匹配 butterflyman、dragonfly man等
app.get(/.*fly$/, function(req, res) {
  res.send('/.*fly$/');
});

var cb0 = function (req, res, next) {
  console.log('CB0')
  next()
}

var cb1 = function (req, res, next) {
  console.log('CB1')
  next()
}

var cb2 = function (req, res) {
  res.send('Hello from C!')
}

app.get('/example/c', [cb0, cb1], function (req, res, next) {
  console.log('response will be sent by the next function ...')
  next()
}, function (req, res) {
  res.send('Hello from D!')
})

app.route('/book')
  .get(function(req, res) {
    res.send('Get a random book');
  })
  .post(function(req, res) {
    res.send('Add a book');
  })
  .put(function(req, res) {
    res.send('Update the book');
  });


app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})
